<?php
require('../include/init.inc.php');
$method = $action = $id = $ck_date = $rk_date = $model = $search = $page_no = '';
extract($_REQUEST, EXTR_IF_EXISTS);


if (Common::isGet() && $search == 1) {
    $search_str = " WHERE 1=1 ";
    if ($model != '') {
        $search_str .= "  AND products_model = '$model'";
    }


    if ($ck_date != '' && $rk_date != '') {
        if (strtotime($rk_date) > time() || strtotime($rk_date) > time()) {
            OSAdmin::alert('error', '查询時間只能相对当前时间往前');
        }

        if (((time() - strtotime($rk_date)) > 30 * 24 * 3600) || ((time() - strtotime($ck_date)) > 30 * 24 * 3600)) {
            OSAdmin::alert('error', '查询時間只能往前且不能超過30天');
        }

        $cktime = strtotime($ck_date);
        $rktime = strtotime($rk_date);
        $search_str .= " AND UNIX_TIMESTAMP(products_due_date) >= $cktime AND UNIX_TIMESTAMP(products_add_date) >= $rktime";
    } elseif ($rk_date != '' && $ck_date == '') {
        if (strtotime($rk_date) > time()) {
            OSAdmin::alert('error', '查询時間只能相对当前时间往前');
        }

        if (((time() - strtotime($rk_date)) > 30 * 24 * 3600)) {
            OSAdmin::alert('error', '查询時間只能往前且不能超過30天');
        }

        $rktime = strtotime($rk_date);
        $search_str .= " AND UNIX_TIMESTAMP(products_add_date) >= $rktime";
    } elseif ($rk_date == '' && $ck_date != '') {
        if (strtotime($ck_date) > time()) {
            OSAdmin::alert('error', '查询時間只能相对当前时间往前');
        }

        if ((time() - strtotime($ck_date)) > (30 * 24 * 3600)) {
            OSAdmin::alert('error', '查询時間只能往前且不能超過30天');
        }
        $cktime = strtotime($ck_date);
        $search_str .= " AND UNIX_TIMESTAMP(products_due_date) >= $cktime";
    }


    $row_count = AdminProducts::getCountBySearch($search_str);
    $row_count = $row_count['c'];
    $page_size = PAGE_SIZE;
    $page_no = $page_no < 1 ? 1 : $page_no;

    $total_page = $row_count % $page_size == 0 ? $row_count / $page_size : ceil($row_count / $page_size);
    $total_page = $total_page < 1 ? 1 : $total_page;
    $page_no = $page_no > ($total_page) ? ($total_page) : $page_no;
    $start = ($page_no - 1) * $page_size;

    $products = AdminProducts::getAllBySearch($search_str, $start, $page_size);
    if ($products) {
//    $products = AdminProducts::getAllProducts();
        foreach ($products as $product) {
            $np['products_id'] = $product['products_id'];
            $np['products_name'] = $product['products_name'];
            $np['products_model'] = $product['products_model'];
            $np['products_description'] = $product['products_description'];
            $np['products_price'] = $product['products_price'];
            $np['products_currency'] = $product['products_currency'];
            $np['products_sale_num'] = $product['products_sale_num'];
            $np['products_sale_other'] = $product['products_sale_other'];
            $np['products_evaluate'] = $product['products_evaluate'];
            $np['products_add_status'] = $product['products_add_status'];
            $np['products_due_status'] = $product['products_due_status'];

            $time = time() - strtotime($product['products_add_date']);
            if (($time <= 10 * 24 * 3600) && $product['products_add_status'] == '1') {
                $np['products_new'] = 1;
            } else {
                $np['products_new'] = 0;
            }

            $np['products_due_date'] = $product['products_due_date'];
            $np['products_add_date'] = $product['products_add_date'];

            $num = AdminProducts::getTenDaysSaleOtherNums();
            if (!$num['sum']) {
                $np['avge'] = "0%";
            } else {
                $np['avge'] = sprintf('%.2f', intval($num['sum'])/ 10) * 100;
                $np['avge'] = $np['avge'] . "%";
            }

            $nps[] = $np;
        }
    } else {
        $nps = array();
    }
} else if ($action == 'del') {
    $ret = AdminProducts::delete($id);
    if ($ret) {
        SysLog::addLog(UserSession::getUserName(), 'Delete', 'ProductsDel', $id);
        Common::exitWithSuccess('删除完成', 'entpot/products.php');
    } else {
        OSAdmin::alert("error");
    }
} else {


    $search_str = " WHERE 1=1 ";
    $row_count = AdminProducts::getCountBySearch($search_str);
    $row_count = $row_count['c'];
    $page_size = PAGE_SIZE;
    $page_no = $page_no < 1 ? 1 : $page_no;

    $total_page = $row_count % $page_size == 0 ? $row_count / $page_size : ceil($row_count / $page_size);
    $total_page = $total_page < 1 ? 1 : $total_page;
    $page_no = $page_no > ($total_page) ? ($total_page) : $page_no;
    $start = ($page_no - 1) * $page_size;
    $products = AdminProducts::getAllBySearch($search_str, $start, $page_size);
    if ($products) {
        foreach ($products as $product) {
            $np['products_id'] = $product['products_id'];
            $np['products_name'] = $product['products_name'];
            $np['products_model'] = $product['products_model'];
            $np['products_description'] = $product['products_description'];
            $np['products_price'] = $product['products_price'];
            $np['products_currency'] = $product['products_currency'];
            $np['products_sale_num'] = $product['products_sale_num'];
            $np['products_sale_other'] = $product['products_sale_other'];
            $np['products_evaluate'] = $product['products_evaluate'];
            $np['products_add_status'] = $product['products_add_status'];
            $np['products_due_status'] = $product['products_due_status'];

            $time = time() - strtotime($product['products_add_date']);
            if (($time <= 10 * 24 * 3600) && $product['products_add_status'] == '1') {
                $np['products_new'] = 1;
            } else {
                $np['products_new'] = 0;
            }

            $np['products_due_date'] = $product['products_due_date'];
            $np['products_add_date'] = $product['products_add_date'];

            $num = AdminProducts::getTenDaysSaleOtherNums();
            if (!$num['sum']) {
                $np['avge'] = "0%";
            } else {
                $np['avge'] = sprintf('%.2f', intval($num['sum'])/ 10) * 100;
                $np['avge'] = $np['avge'] . "%";
            }

            $nps[] = $np;
        }
    } else {
        $nps = array();
    }
}


$page_html = Pagination::showPager("products.php?search=1&model=$model&rk_date=$rk_date&ck_date=$ck_date", $page_no, PAGE_SIZE, $row_count);

$confirm_html = OSAdmin::renderJsConfirm("icon-remove");
Template::assign('osadmin_action_confirm', $confirm_html);
Template::assign('_GET', $_GET);
Template::assign('page_no', $page_no);
Template::assign('current_user', UserSession::getUserGroup());
Template::assign('page_html', $page_html);
Template::assign('products', $nps);
Template::display('entpot/products.tpl');